﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;
namespace WindowsGame4
{
    public class Player : Damageable
    {
        public bool bBeginSet = false, bEndSet = false;
        public float curEnergy, maxEnergy, maxSpeed=750;
        public TouchCollection touch;
        public GameObject cursor, saucer;
        public Vector2 shipDrag, aimTarget, pinchBegin, pinchEnd;
        public Weapon weapon;
        public const int touchWidth=30;
        public ParticleEmitter thrusters;
        public List<Vector2> aims;
        public int minerals, specimens;
        public int saucerLevel;

        public Player()
            : base()
        {
            aims = new List<Vector2>();
            curEnergy = maxEnergy = 100;
            texture = GameManager.cockpit;
            minerals = 0;
            specimens = 0;
            bBlocks = true;
            weapon = new RayGun(this);
            weapon.SetLevel(0);
            depth = .01f;
            Vector2 scale = new Vector2(.2f);
            this.scale = scale;

            saucer = new GameObject();
            saucer.attachTarget = this;
            saucer.texture = GameManager.saucer;
            saucer.depth = .01f;
            saucer.scale = scale;
            saucer.bCollides = false;
            GameManager.AddObject(saucer);
           
            thrusters = new ExhaustEmitter();
            thrusters.attachTarget = this;
            thrusters.color = new Vector4(0, 1, 1, 1);
            GameManager.AddObject(thrusters);
            bBlocks = true;

        }
        public void Input()
        {
            if (bDead) 
                return;
            ParseTouch();
            SetAim();
            Vector2 dir = pos - shipDrag;
            saucer.angle = GameObject.GetAngle(dir) + 90;
            dir=aimTarget-pos;
            angle = GameObject.GetAngle(dir) - 90;
            weapon.angle = GameObject.GetAngle(dir);
            if (!Touches(shipDrag, this))
                Thrust();
            else StopThrust();
            
            if(!Touches(aimTarget, this))
                if (weapon.bIsFiring)
                    weapon.Fire(aimTarget);
                else weapon.BeginFire(aimTarget);
            else if (weapon.bIsFiring)
            {
                bBeginSet = false;
                bEndSet = false;
                weapon.EndFire(aimTarget);
            }
        }
        public void ParseTouch()
        {

            aims.Clear();
            touch = TouchPanel.GetState();
            bool bShipDrag=false, bAim=false;
            pinchBegin = GetGlobal(new Vector2(800, 480));
            foreach (TouchLocation tl in touch)
            {
                Vector2 vt = GetGlobal(new Vector2(tl.Position.X, tl.Position.Y));
                if (Touches(vt, shipDrag, touchWidth))
                {
                    shipDrag = vt;
                    bShipDrag = true;
                }
                else 
                {
                    aims.Add(vt);
                    /*if (!bAim)
                    {
                        bAim = true;
                        aimTarget = vt;
                    }
                    if (bBeginSet && Touches(pinchBegin, vt, touchWidth))
                        pinchBegin = vt;
                    else if (bEndSet && Touches(pinchEnd, vt, touchWidth))
                        pinchEnd = vt;
                    else if (Touches(vt, aimTarget, touchWidth))
                        aims.Add(vt);*/
                    
                }
            }

            
            if (!bShipDrag)
                shipDrag = pos;
            if (!bAim)
                aimTarget = pos;
        }
        public void SetAim()
        {
            bBeginSet = bEndSet = false;
            bBeginSet = true;
            pinchBegin = pos;
            foreach (Vector2 p in aims)
            {
                if (weapon is RayGun)
                {
                    aimTarget = p;
                    break;
                }
                /*if (!bBeginSet)
                {
                    bBeginSet = true;
                    pinchBegin = p;
                }
                else*/ if (!bEndSet)
                {
                    bEndSet = true;
                    pinchEnd = p;
                }
                       if (bEndSet && bBeginSet)
                           aimTarget = (pinchBegin + pinchEnd) * .5f;
            }
            if (weapon is WaveGun)
            {
                WaveGun gun = (WaveGun)weapon;
                if (bBeginSet)
                    gun.begin = pinchBegin;
                else if (bEndSet)
                    gun.end = pinchEnd;
                if (bBeginSet && bEndSet)
                    aimTarget = pinchBegin + (pinchEnd - pinchBegin) * .5f;
                else aimTarget = pos;
            }
        }
        public void Thrust()
        {
            Vector2 dir = shipDrag - pos;
            float tempAngle=GetAngle(dir);
            thrusters.SetEmissionRate(20);
            thrusters.moveSpeed=-dir.Length();
            thrusters.minAngle = tempAngle - 30;
            thrusters.maxAngle = tempAngle + 30;
            thrusters.color = new Vector4(1);
            acc = -dir;
        }
        public override void OnDeath()
        {
            base.OnDeath();
            thrusters.minAngle = 0;
            thrusters.maxAngle = 360;
            thrusters.nEmits = 30;
            thrusters.SetEmissionRate(0);
            thrusters.target = this;
            thrusters.moveSpeed = -200;
            thrusters.bOntimer = true;
            thrusters.lifeTime = 1;
            saucer.bDead = true;
            GameManager.explosion.Play();
            weapon.OnDeath();
        }
        public void StopThrust()
        {
            acc = new Vector2();
            thrusters.SetEmissionRate(-1);
        }
        public Vector2 GetCursorPos()
        {
            return shipDrag;
        }
        public bool Touches(Vector2 v1, GameObject obj)
        {
            Vector2 vd = v1 - obj.pos;
            return vd.Length() <= 2 * obj.GetRadius();
        }
        public bool Touches(Vector2 v1, Vector2 v2, float vwidth)
        {
            Vector2 vd = v1 - v2;
            return vd.Length() <= 2 * vwidth;
        }
        public override void Update(float time)
        {
            Vector2 dir = vel;
            float mag = vel.Length();
            if (mag > 0)
                dir.Normalize();
            vel = dir * (float)Math.Min(mag, maxSpeed);
            base.Update(time);
            int level = (int)Math.Ceiling((float)specimens / 10.0f);
            GameManager.SetPoliceLevel(level);
        }
        public override void React(GameObject obj, Vector2 cp)
        {
            base.React(obj, cp);
        }
        public void purchase(int mineralsSpent, int specimenSpent)
        {
            minerals -= mineralsSpent;
            specimens -= specimenSpent;
        }
        public void setWeapon(int level)
        {
            weapon.SetLevel(level);
        }
        public void setSaucer(int level)
        {
            curHealth = 100 * (level + 1);
            saucerLevel = level;
        }
        public void setExhaust(int level)
        {
            //DEFINE THIS METHOD
        }

    }
}
